package com.itany.controller;


import com.github.pagehelper.PageInfo;
import com.itany.exception.DisabledException;
import com.itany.exception.EnabledException;
import com.itany.pojo.User;
import com.itany.service.UserService;
import com.itany.vo.ActionResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.awt.font.MultipleMaster;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户控制器
 *
 * @author Tom
 * @date 2021/06/26
 */
@Controller
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	UserService userService;
	
	@RequestMapping("/findAll")
	@ResponseBody
	public Map<String,Object> findAll(@RequestParam(defaultValue="1")Integer page,
									  @RequestParam(defaultValue="10")Integer rows){
		Map<String,Object> map = new HashMap<String, Object>();
		PageInfo<User> info = userService.findUserAll(page, rows);
		map.put("total", info.getTotal());
		map.put("rows", info.getList());
		return map;
	}

	@RequestMapping("/modify")
	@ResponseBody
	public ActionResult modify(Integer id,String username, String phone, String email, String interest) {

		userService.modifyById(id, username, phone, email, interest);
		return ActionResult.success();
	}

	@RequestMapping("/modifyStatusDisable")
	@ResponseBody
	public ActionResult modifyStatusDisable(Integer id) {
		ActionResult result = null;
		try {
			userService.modifyStatusDisable(id);
			result =  ActionResult.success();
		} catch (DisabledException e) {
			result = ActionResult.userDisabled(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			result = ActionResult.fail("服务器内部异常");
		}

		return result;
	}

	@RequestMapping("/modifyStatusEnable")
	@ResponseBody
	public ActionResult modifyStatusEnable(Integer id) {
		ActionResult result = null;
		try {
			userService.modifyStatusEnable(id);
			result =  ActionResult.success();
		} catch (EnabledException e) {
			result = ActionResult.userEnabled(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			result = ActionResult.fail("服务器内部异常");
		}

		return result;
	}

	@RequestMapping("/findByName")
	@ResponseBody
	public Map<String,Object> findByName(@RequestParam(defaultValue="1")Integer page,
									  @RequestParam(defaultValue="5")Integer rows, String keyword,@RequestParam("search_type") Integer searchType){
		Map<String,Object> map = new HashMap<String, Object>();
		PageInfo<User> info = userService.findByName(page, rows, keyword, searchType);
		map.put("total", info.getTotal());
		map.put("rows", info.getList());
		return map;
	}


}
